
const sync = require('./utils/syncRequestUtil')

App({
    onLaunch() {

        // 展示本地存储能力
        const logs = wx.getStorageSync('logs') || []
        logs.unshift(Date.now())
        wx.setStorageSync('logs', logs)

        //存储头部导航栏高度：单位 px  （注意是px）
        let wxBarHeight = wx.getSystemInfoSync().statusBarHeight + 40
        wx.setStorageSync('wxBarHeight', wxBarHeight)
        // console.log("头部导航栏高度:", wxBarHeight)
        //存储页面二级菜单高度：80 rpx
        wx.setStorageSync('submenu', 80)
        // console.log("页面二级菜单高度：", wx.getStorageSync('submenu'))


        // 登录
        wx.login({
            success: res => {
                // 发送 res.code 到后台换取 openId, sessionKey, unionId
            }
        })

        let id = wx.getStorageSync('userId')
        let status = wx.getStorageSync('status')


        //先发一次请求获取tarbar的徽标
        
        wx.request({
            url: `http://120.77.156.205:8808/wx/disease/getDiseaseVoAmount/${id}/0`,
            method: 'GET',
            success: (res) => {
                if (res.data.data.count != 0) {
                    wx.setTabBarBadge({
                        index: 2,
                        text: String(res.data.data.count),
                    })
                } else {
                    //如果数量为0，隐藏徽标提示
                    wx.removeTabBarBadge({
                        index: 2,
                    })
                    
                }
            }
        })

        let interval= setInterval(() => {
            id = wx.getStorageSync('userId')
            status = wx.getStorageSync('status')

            // console.log("status", status)
            const pages = getCurrentPages();
            // console.log(pages)
            const route = pages[pages.length - 1].route;
            //如果已登录,且是专家
            if (id && status == 2) {

                // console.log(route)
                // 如果当前页面路径是tarbar页面，则每隔3s更新专家系统的徽标
                if (route == "pages/index/index" || route == "pages/installation/installation" || route == "pages/systems/systems" || route == "pages/other/other") {

                    // console.log("id && status == 2  ", "更新")
                    wx.request({
                        url: `http://120.77.156.205:8808/wx/disease/getDiseaseVoAmount/${id}/0`,
                        method: 'GET',
                        success: (res) => {
                            if (res.data.data.count != 0) {
                                wx.setTabBarBadge({
                                    index: 2,
                                    text: String(res.data.data.count),
                                })
                            } else {
                                //如果数量为0，隐藏徽标提示
                                wx.removeTabBarBadge({
                                    index: 2,
                                })
                                
                            }

                        }
                    })

                } else {
                    // console.log("不是tarbar")
                }
            } else if (id && status != 2) {
                
                if (route == "pages/index/index" || route == "pages/installation/installation" || route == "pages/systems/systems" || route == "pages/other/other") {
                    // console.log("id && status ！= 2  ", "更新")
                    wx.removeTabBarBadge({
                        index: 2,
                    })
                }
            }
        }, 2000);

    },
    globalData: {
        sync,
        userInfo: null
    },
})
